package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.Nota;

/**
 * Servlet implementation class ReportServletResult
 */
@WebServlet("/ReportServletResult")
public class ReportServletResult extends HttpServlet {
	private static final long serialVersionUID = 1L;

	EntityManagerFactory emf;
	EntityManager em;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ReportServletResult() {
		super();
		// TODO Auto-generated constructor stub
	}

	public void init() {

		emf = Persistence.createEntityManagerFactory("ServletTest");
		em = emf.createEntityManager();

	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession(true);

		String snume = (String) session.getAttribute("studNume");

		String date = request.getParameter("dateIndex");

		List<Timestamp> dat = em
				.createQuery(
						"select n.data from Nota n where n.student.nume= :snume")
				.setParameter("snume", snume).getResultList();

		Timestamp choosen = null;

		for (Timestamp tmp : dat) {
			if (tmp.toString().equals(date.toString())) {
				choosen = tmp;
			}
		}

		PrintWriter out = response.getWriter();

		List<Nota> note = em
				.createQuery(
						"select n from Nota n where n.student.nume= :snume and n.data = :ndata")
				.setParameter("snume", snume).setParameter("ndata", choosen)
				.getResultList();

		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
				+ "Transitional//EN\">");
		out.println("<html>");

		out.println("<head>");
		out.println("<title>" + "Main page!" + "</title>");
		out.println("</head>");
		out.println("<body bgcolor=\"#F3FBFE\">");
		out.println("<br> Acestea sunt notele din data de " + choosen);
		out.println("<br> " + snume);

		for (Nota temp : note) {
			out.println("<br >" + temp);
		}

		out.println("<form method=\"GET\" action=\"/ServletTest/DispatcherServlet\"> ");
		out.println("<br><br><input type=\"submit\" name=\"main\" value=\"Main\"></form>");

		out.println("</body>");
		out.println("</html>");

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
